package com.ruoyi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.domain.TestKnowledge;
import com.ruoyi.domain.vo.TestKnowledgeVo;
import com.ruoyi.domain.vo.TestSubjectGradeVo;
import com.ruoyi.mapper.TestKnowledgeMapper;
import com.ruoyi.service.ITestKnowledgeService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 考试平台知识点Service业务层处理
 */
@Service
public class TestKnowledgeServiceImpl extends ServiceImpl<TestKnowledgeMapper,TestKnowledge> implements ITestKnowledgeService
{
    @Autowired
    private TestKnowledgeMapper testKnowledgeMapper;

    /**
     * 查询考试平台知识点详情
     *
     * @param knowlId 考试平台知识点主键
     * @return 考试平台知识点
     */
    @Override
    public TestKnowledge selectTestKnowledgeByKnowlId(Long knowlId)
    {
        return testKnowledgeMapper.selectTestKnowledgeByKnowlId(knowlId);
    }

    /**
     * 查询考试平台知识点列表
     *
     * @param grade 年级
     * @return 考试平台知识点
     */
    @Override
    public List<TestSubjectGradeVo> selectTestKnowledgeList(String grade)
    {
        return testKnowledgeMapper.selectTestKnowledgeList(grade);
    }

    /**
     * 查询考试平台知识点列表 用于判断新增知识点是否重复
     *
     * @param testKnowledge 考试平台知识点
     * @return 考试平台知识点
     */
    @Override
    public List<TestSubjectGradeVo> selectTestKnowledgeListByClass(TestKnowledge testKnowledge) {
        return testKnowledgeMapper.selectTestKnowledgeListByClass(testKnowledge);
    }

    /**
     * 新增考试平台知识点
     *
     * @param testKnowledge 考试平台知识点
     * @return 结果
     */
    @Override
    public int insertTestKnowledge(TestKnowledge testKnowledge)
    {
        return testKnowledgeMapper.insertTestKnowledge(testKnowledge);
    }

    /**
     * 修改考试平台知识点
     *
     * @param testKnowledge 考试平台知识点
     * @return 结果
     */
    @Override
    public int updateTestKnowledge(TestKnowledge testKnowledge)
    {
        return testKnowledgeMapper.updateTestKnowledge(testKnowledge);
    }

    /**
     * 查询考试平台知识点子节点
     *
     * @param knowlId 考试平台知识点主键
     * @return 考试平台知识点
     */
    @Override
    public List<TestKnowledge> selectTestKnowledgeChildId(Long knowlId) {
        return testKnowledgeMapper.selectTestKnowledgeChildId(knowlId);
    }


    /**
     * 查询所有考试平台知识点
     */
    @Override
    public List<TestKnowledgeVo> listWithTree(String grade,String subjectName) {
        List<TestKnowledgeVo> entities = testKnowledgeMapper.selectTestKnowledges(grade,subjectName); //这个可以写成你自己后台接口所取到数据的方法，由于我使用的是Mybatis-plus直接调用这个方法取得数据

//把TestKnowledge的数组entities包装成knowledges数组
        List<TestKnowledgeVo> knowledges = entities.stream().map(knowledgeVo -> {
            TestKnowledgeVo knowledge = new TestKnowledgeVo();
            BeanUtils.copyProperties(knowledgeVo, knowledge);
            return knowledge;
        }).collect(Collectors.toList());


//filter是java8的写法，取得所有parentId为0的数据，也就是一级目录
//map也是java8的写法，用于封装数据，取得他的孩子（也就是下级目录）的数据
        List<TestKnowledgeVo> list = knowledges.stream().filter(knowledge ->
                knowledge.getParentId().equals(0)
        ).map((menu)->{
            menu.setChildList(getChildrenData(menu,knowledges));
            return menu;
        }).collect(Collectors.toList());
        return list;
    }

    //获取孩子（下级目录）的方法，递归实现
    private List<TestKnowledgeVo> getChildrenData(TestKnowledgeVo root, List<TestKnowledgeVo> all) {
        List<TestKnowledgeVo> children = all.stream().filter(knowledge ->
                knowledge.getParentId().equals(root.getKnowlId())
        ).map(knowledge -> {
            knowledge.setChildList(getChildrenData(knowledge,all));
            return knowledge;
        }).collect(Collectors.toList());
        return children;
    }

}
